package hotel.action;

import static common.utils.Constants.LIMIT;
import hotel.service.IHotelOrderService;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import common.action.BaseAction;
import common.pojo.HotelOrder;

import common.utils.Page;
import common.utils.PageUtil;
import common.utils.PaginationUtil;

@Scope("prototype")
@Controller("hotelOrderAction")
public class HotelOrderAction extends BaseAction {

	/**
	 * 分页查询
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getHotelOrderListByPage() throws Exception {
		String currentTmp = request.getParameter("current");
		String stateTmp = request.getParameter("state");
		String where =getWhere(stateTmp);
		Page page = getPage(currentTmp, where);
		hotelOrderList = hotelOrderService.find(page.getBeginIndex(), LIMIT,
				"orderTime desc", where);
		pagination = PaginationUtil.getPaginationStyleTwo(page,
				"getHotelOrderListByPage.action?state=" + state);
		return "hotelOrderListByPage";
	}
	
	private String getWhere(String stateTmp) {
		Timestamp ts = new Timestamp(System.currentTimeMillis());
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
		SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
		SimpleDateFormat df3 = new SimpleDateFormat("yyyy-MM-dd 18:00:00");
		SimpleDateFormat df4 = new SimpleDateFormat("yyyy-MM-dd 16:00:00");
		String currenttime = format.format(ts);
		String time = df.format(ts);
		String time2 = df2.format(ts);
		String time3 = df3.format(ts);
		String time4 = df4.format(ts);
		if (stateTmp != null) {
			state = Integer.valueOf(stateTmp);
		} else {
			state = 0;
		}
		String where = null;
		//订单查询，状态
		switch (state) {
		case 0:
			where = " verify=1";
			break;
		case 1:
			where = "tradeStatus=1  and verify=1";
			break;
		case 2:
			where = "tradeStatus=2  and verify=1";
			break;
		case 3:
			where = "tradeStatus=3  and verify=1";
			break;
		case 4:
			where = "tradeStatus=4  and verify=1";
			break;
		case 5:
			where = "tradeStatus=5 ";
			break;
		case 6:
			where = "checkinTime>='" + time + "'  and checkinTime<='" + time2
					+ "' and verify=1 and tradeStatus!=5";
			break;
		case 7:
			//判断是否在4点到6点
			if (currenttime.compareTo(time4) > 0
					&& currenttime.compareTo(time3) < 0)
				where = " verify=1 and tradeStatus in(1,2) and checkinTime='"+currenttime.substring(0, currenttime.indexOf(" "))+"'";
			else
				where = " 1=2 ";
			break;
		case 8:
			//判断是否超过6点
			if (currenttime.compareTo(time3) > 0)
				where =  " verify=1 and tradeStatus in(1,2) and checkinTime='"+currenttime.substring(0, currenttime.indexOf(" "))+"'";
			else
				where = " 1=2 ";
			break;
		}
		return where;
	}

	/**
	 * 查询符合条件的订单
	 */
	public String search() throws Exception {
		String where = " verify=1 ";
		String search = request.getParameter("search");
		String startDate = request.getParameter("startDate");
		int state = Integer.parseInt(request.getParameter("state"));

		if (!StringUtils.isBlank(startDate))
			where += " and checkinTime='" + startDate + " 14:00:00'";
		if (state != 0) 
			where += " and tradeStatus=" + state;
		if (!StringUtils.isBlank(search)) {
			where += " and ( code like '%" + search
					+ "%' or customerName like '%" + search + "%')";
		}
		String currentTmp = request.getParameter("current");
		Page page = getPage(currentTmp, where);
		hotelOrderList = hotelOrderService.find(page.getBeginIndex(), LIMIT,
				"orderTime desc", where);
		pagination = PaginationUtil.getPaginationStyleTwo(page,
				"search.action?startDate=" + startDate +"&search=" + search + "&state=" + state);
		return "hotelOrderListByPage";
	}

	public String getVerifyListByPage() throws Exception {

		String currentTmp = request.getParameter("current");
		String where = " verify=0 and tradeStatus!=5 ";
		Page page = getPage(currentTmp, where);
		hotelOrderList = hotelOrderService.find(page.getBeginIndex(), LIMIT,
				"orderTime desc", where);
		pagination = PaginationUtil.getPaginationStyleTwo(page,
				"getVerifyListByPage.action");
		return "verifyListByPage";
	}

	public Page getPage(String currentTmp, String where) {
		int total= hotelOrderService.getCount(where);
		int current=PageUtil.getCurrent(currentTmp);
		Page page = PageUtil.createPage(LIMIT, total, current);
		return page;
	}

	/**
	 * 根据id查找订单
	 * 
	 * @return
	 */
	public String findInfoById() {
		int id = Integer.valueOf(request.getParameter("id"));
		hotelOrder = hotelOrderService.findInfoById(id);
		return "findSuccess";
	}

	/**
	 * 审核
	 */
	public String ispass() {
		String ids = request.getParameter("ids");
		hotelOrderService.verify(ids);
		return SUCCESS;
	}

	/**
	 * 更新订单内容
	 * 
	 * @return
	 */
	public String updateOrder() {

		hotelOrderService.update(hotelOrder);
		url = "panel/hotel/hotelOrder/getHotelOrderListByPage.action";
		return SUCCESS;
	}


	/**
	 * 修改订单状态
	 */
	public String updateState() {
		int id = Integer.valueOf(request.getParameter("id"));
		int state = Integer.valueOf(request.getParameter("state"));
		hotelOrderService.updateState(id, state);
		return SUCCESS;
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	private IHotelOrderService hotelOrderService;
	private List<HotelOrder> hotelOrderList;
	private String pagination;
	private HotelOrder hotelOrder;
	private int state;
	
	
	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

	public HotelOrder getHotelOrder() {
		return hotelOrder;
	}

	public void setHotelOrder(HotelOrder hotelOrder) {
		this.hotelOrder = hotelOrder;
	}

	public String getPagination() {
		return pagination;
	}

	public void setPagination(String pagination) {
		this.pagination = pagination;
	}

	public List<HotelOrder> getHotelOrderList() {
		return hotelOrderList;
	}

	public void setHotelOrderList(List<HotelOrder> hotelOrderList) {
		this.hotelOrderList = hotelOrderList;
	}

}
